package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.TypeDao;
import com.aaa.qy127.entity.Type;
import com.aaa.qy127.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository("typeDaoImpl")
public class TypeDaoImpl implements TypeDao {
    /**
     * 查询全部书籍类型的信息
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllType() {
        String sql = "select typeid,typename from t_type";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql);
        if (maps.size()>0){
            return maps;
        }
        return null;
    }
    @Override
    public List<Map<String, Object>> queryAllType(Type types) {
        String sql = "select tt.typeid,tt.typename,count(tb.typeid) tcount\n" +
                "from t_type tt \n" +
                "LEFT JOIN t_book tb \n" +
                "ON tb.typeid=tt.typeid \n" +
                "GROUP BY tt.typeid\n" +
                "limit ?,?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, (types.getPage() - 1) * types.getLimit(), types.getLimit());
        if (maps.size() >0) {
            return maps;
        }
        return null;
    }

    @Override
    public int insert(Type types) {
        String sql = "insert into t_type values (null,?)";
        int i = JDBCUtil.update(sql,types.getTypename());
        return i;
    }

    @Override
    public int updateType(Type types) {
        String sql = "update t_type set typename=? where typeid=?";
        int i = JDBCUtil.update(sql,types.getTypename(),types.getTypeid());
        return i;
    }

    @Override
    public int getAllCount() {
        String sql = "select count(typeid) from t_type ";
        List<List> lists = JDBCUtil.queryList(sql);
        if (lists.size() >0 ) {
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }

    @Override
    public int delType(Type types) {
        String sql = "delete from t_type where typeid=?";
        int i = JDBCUtil.update(sql, types.getTypeid());
        return i;
    }

    /**
     * 查询 所有的 书籍类型 并分页
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllLimit() {
        String sql = "select typeid,typename from t_type LIMIT 0,9";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql);
        if (maps.size()>0){
            return maps;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> selTypes() {
        String sql = "select typename from t_type";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql);
        return maps;
    }

    @Override
    public List<Map<String, Object>> selTypename(String typename) {
        String sql = "select typename from t_type where typename=?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, typename);
        return maps;
    }
}
